package es.unex.meigas.core;

import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.util.Date;

import es.unex.meigas.gui.World2Device;


public class Cruise extends DasocraticElement {

	private Date m_Date = new Date();

	public Cruise(){

		super();

		setName("Nuevo muestreo");

	}

	protected DasocraticElement _addElement(DasocraticElement element){

		if (element instanceof Plot){
			element.setParent(this);
			m_Elements.add(element);
			return this;
		}
		else{
			return null;
		}

	}

	public Rectangle2D getBoundingBox() {

		int i;
		DasocraticElement element;
		Rectangle2D rect;
		double dMinX = Double.MAX_VALUE;
		double dMaxX = Double.NEGATIVE_INFINITY;
		double dMinY = Double.MAX_VALUE;
		double dMaxY = Double.NEGATIVE_INFINITY;

		for (i = 0; i < m_Elements.size(); i++){
			element = (DasocraticElement)m_Elements.get(i);
			rect = element.getBoundingBox();
			dMinX = Math.min(rect.getMinX(), dMinX);
			dMinY = Math.min(rect.getMinY(), dMinY);
			dMaxX = Math.max(rect.getMaxX(), dMaxX);
			dMaxY = Math.max(rect.getMaxY(), dMaxY);
		}

		return new Rectangle2D.Double(dMinX, dMinY,
									  dMaxX - dMinX,
									  dMaxY - dMinY);

	}

	public void paint(Graphics g, World2Device w2d) {

		int i;

		for (i = 0; i < m_Elements.size(); i++){
			((DasocraticElement)m_Elements.get(i)).paint(g, w2d);
		}

	}

	public double getArea() {

		int i;
		double dValue;
		double dArea = 0;

		for (i = 0; i < m_Elements.size(); i++){
			dValue = ((DasocraticElement)m_Elements.get(i)).getArea();
			if (dValue != NO_DATA){
				dArea += dValue;
			}
		}

		if (dArea == 0){
			dArea = NO_DATA;
		}

		return dArea;

	}

	public Date getDate() {

		return m_Date;

	}

	public void setDate(Date date) {

		if (!date.equals(m_Date)){
			m_Date = date;
			rootHasChanged(true);
		}

	}

	public String[] getReport() {

		return null;

	}

	public DasocraticElement getNewInstance(){

		Cruise cruise = (Cruise) super.getNewInstance();
		cruise.setDate(m_Date);

		return cruise;

	}


}
